import { GetChildrenList_API, GetTownKeyValue_API } from '@/server/home'

/**
 * 获取乡镇选项的 Hook
 */
export const useTownOptions = () => {
    const townOptions = ref([])

    const fetchTownOptions = async () => {
        const res = await GetTownKeyValue_API()
        if (res.IsSuccess) {
            townOptions.value = res.Data.map((item) => ({
                text: item.Value,
                value: item.Key,
            }))
        }
    }

    return {
        townOptions: computed(() => townOptions.value),
        fetchTownOptions,
    }
}

/**
 * 获取子用户列表的 Hook
 */
export const useUserChildrenList = () => {
    const childrenList = ref([])

    const fetchUserChildrenList = async (usertype) => {
        const res = await GetChildrenList_API(usertype)
        if (res.IsSuccess) {
            childrenList.value = res.Data.map((item) => ({
                text: item.RealName,
                value: item.Id,
                AdminCode: item.AdminCode,
                TownCode: item.TownCode,
            }))
        }
    }

    return {
        childrenList: computed(() => childrenList.value),
        fetchUserChildrenList,
    }
}
